package com.hhrpc.hhrpc.core.utils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;

public class TypeUtils {
    public static Class<?>cast(Class<?>clazz){
        if(clazz==null){
            return null;
        }
        if(Integer.class.isAssignableFrom(clazz)||Integer.TYPE.equals(clazz)){
            return Integer.class;
        }
        if(Short.class.isAssignableFrom(clazz)||Short.TYPE.equals(clazz)){
            return Short.class;
        }
        if(Character.class.isAssignableFrom(clazz)||Character.TYPE.equals(clazz)){
            return Character.class;
        }
        if(Long.class.isAssignableFrom(clazz)||Long.TYPE.equals(clazz)){
            return Long.class;
        }
        if(Boolean.class.isAssignableFrom(clazz)||Boolean.TYPE.equals(clazz)){
            return Boolean.class;
        }
        if(Double.class.isAssignableFrom(clazz)||Double.TYPE.equals(clazz)){
            return Double.class;
        }
        if(Float.class.isAssignableFrom(clazz)||Float.TYPE.equals(clazz)){
            return Float.class;
        }
        return clazz;
    }

    private static Object cast(Object origin,Class<?>paramType,Type genericParameterType){
        if(origin==null){
            return null;
        }
        if(Integer.class.isAssignableFrom(paramType)||Integer.TYPE.equals(paramType)){
            return Integer.valueOf(origin.toString());
        }
        if(Short.class.isAssignableFrom(paramType)||Short.TYPE.equals(paramType)){
            return Short.valueOf(origin.toString());
        }
        if(Character.class.isAssignableFrom(paramType)||Character.TYPE.equals(paramType)){
            return Character.valueOf(origin.toString().charAt(0));
        }
        if(Long.class.isAssignableFrom(paramType)||Long.TYPE.equals(paramType)){
            return Long.valueOf(origin.toString());
        }
        if(Boolean.class.isAssignableFrom(paramType)||Boolean.TYPE.equals(paramType)){
            return Boolean.valueOf(origin.toString());
        }
        if(Double.class.isAssignableFrom(paramType)||Double.TYPE.equals(paramType)){
            return Double.valueOf(origin.toString());
        }
        if(Float.class.isAssignableFrom(paramType)||Float.TYPE.equals(paramType)){
            return Float.valueOf(origin.toString());
        }
        Gson gson=new Gson();
        String json = gson.toJson(origin);
        //jdk8以下代码无法执行   需要更新jdk版本
//        if(List.class.isAssignableFrom(paramType)){
//            if (genericParameterType instanceof ParameterizedType parameterizedType) {
//                Type[]actualTypeArguments=parameterizedType.getActualTypeArguments();
//                TypeToken<?>paramterized=TypeToken.getParameterized(List.class,actualTypeArguments);
//                return gson.fromJson(json,paramterized.getType());
//            }
//        }
        Object result = gson.fromJson(json, paramType);
        return result;
    }


    public static Object[] processArgs(Object[] args, Class<?>[] parameterTypes, Type[]genericParameterTypes) {
        if(args==null||args.length==0){
            return null;
        }
        Object[]result=new Object[args.length];
        for(int i=0;i< args.length;i++){
            result[i]=cast(args[i],parameterTypes[i],genericParameterTypes[i]);
        }
        return result;
    }
}
